<!DOCTYPE html>
<html lang="en">
  <head>
    <title>UIBarButtonItem Extension Reference</title>
    <link rel="stylesheet" type="text/css" href="../css/jazzy.css" />
    <link rel="stylesheet" type="text/css" href="../css/highlight.css" />
    <meta charset='utf-8'>
    <script src="../js/jquery.min.js" defer></script>
    <script src="../js/jazzy.js" defer></script>
    
  </head>
  <body>
    <a name="//apple_ref/swift/Extension/UIBarButtonItem" class="dashAnchor"></a>
    <a title="UIBarButtonItem Extension Reference"></a>
    <header>
      <div class="content-wrapper">
        <p><a href="../index.html">Closures Docs</a></p>
        <p class="header-right"><a href="https://github.com/vhesener/Closures"><img src="../img/gh.png"/>View on GitHub</a></p>
      </div>
    </header>
    <div class="content-wrapper">
      <p id="breadcrumbs">
        <a href="../index.html">Closures Reference</a>
        <img id="carat" src="../img/carat.png" />
        UIBarButtonItem Extension Reference
      </p>
    </div>
    <div class="content-wrapper">
      <nav class="sidebar">
        <ul class="nav-groups">
          <li class="nav-group-name">
            <a href="../Controls.html">Controls</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="../Extensions/UIButton.html">UIButton</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UISwitch.html">UISwitch</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UITextField.html">UITextField</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UIDatePicker.html">UIDatePicker</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UIPageControl.html">UIPageControl</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UISegmentedControl.html">UISegmentedControl</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UIStepper.html">UIStepper</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UISlider.html">UISlider</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UIControl.html">UIControl</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UIBarButtonItem.html">UIBarButtonItem</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="../Scrolling Views.html">Scrolling Views</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="../Extensions/UITableView.html">UITableView</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UICollectionView.html">UICollectionView</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UIPickerView.html">UIPickerView</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UIScrollView.html">UIScrollView</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="../Gesture Recognizers.html">Gesture Recognizers</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="../Extensions/UIGestureRecognizer.html">UIGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UITapGestureRecognizer.html">UITapGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UILongPressGestureRecognizer.html">UILongPressGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UIPinchGestureRecognizer.html">UIPinchGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UIPanGestureRecognizer.html">UIPanGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UISwipeGestureRecognizer.html">UISwipeGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UIRotationGestureRecognizer.html">UIRotationGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UIScreenEdgePanGestureRecognizer.html">UIScreenEdgePanGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UIView.html">UIView</a>
              </li>
              <li class="nav-group-task">
                <a href="../Gesture Recognizers.html#/s:8Closures9configure7gesture7handleryx_yxctSo19UIGestureRecognizerCRbzlF">configure(gesture:handler:)</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="../Controllers.html">Controllers</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="../Extensions/UIImagePickerController.html">UIImagePickerController</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UIImagePickerController/MediaFilter.html">– MediaFilter</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UIImagePickerController/Result.html">– Result</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="../KVO.html">KVO</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="../Extensions/_5FKeyValueCodingAndObserving.html">_KeyValueCodingAndObserving</a>
              </li>
            </ul>
          </li>
        </ul>
      </nav>
      <article class="main-content">
        <section>
          <section class="section">
            <h1>UIBarButtonItem</h1>
              <div class="declaration">
                <div class="language">
                  <pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">extension</span> <span class="kt">UIBarButtonItem</span></code></pre>

                </div>
              </div>
            <p><code>Closures</code> framework adds closures for <code>UIBarButtonItem</code> tap events, usually
found in a UINavigationBar.</p>

<p>All initializers that support the target-action pattern now have an equivalent
initialier that contains a <code>handler</code> parameter.</p>
<pre class="highlight swift"><code><span class="n">navigationItem</span><span class="o">.</span><span class="n">leftBarButtonItem</span> <span class="o">=</span> <span class="kt">UIBarButtonItem</span><span class="p">(</span><span class="nv">title</span><span class="p">:</span> <span class="s">"Left item"</span><span class="p">,</span> <span class="nv">style</span><span class="p">:</span> <span class="o">.</span><span class="n">plain</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// left bar button item tapped</span>
<span class="p">}</span>
</code></pre>

<p>To add the closure handler to an existing <code>UIBarButtonItem</code>, simply call the
<code>onTap(handler:)</code> method. For instance, if you created your button
in a storyboard, you could call the following in your <code>viewDidLoad</code> method.</p>
<pre class="highlight swift"><code><span class="k">let</span> <span class="nv">myRightBarButton</span> <span class="o">=</span> <span class="n">navigationItem</span><span class="o">.</span><span class="n">rightBarButtonItem</span><span class="o">!</span>
<span class="n">myRightBarButton</span><span class="o">.</span><span class="n">onTap</span> <span class="p">{</span>
    <span class="c1">// right bar button item tapped</span>
<span class="p">}</span>
</code></pre>

          </section>
          <section class="section task-group-section">
            <div class="task-group">
              <ul>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:So15UIBarButtonItemC8ClosuresE5image5style7handlerABSo7UIImageCSg_So0abC5StyleVyyctcfc"></a>
                    <a name="//apple_ref/swift/Method/init(image:style:handler:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:So15UIBarButtonItemC8ClosuresE5image5style7handlerABSo7UIImageCSg_So0abC5StyleVyyctcfc">init(image:style:handler:)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>A convenience initializer for a UIBarButtonItem so that the tap event can
be handled with a closure. This is equivalent of using the init(image:style:target:action:)
initializer.</p>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">convenience</span> <span class="nf">init</span><span class="p">(</span><span class="nv">image</span><span class="p">:</span> <span class="kt">UIImage</span><span class="p">?,</span> <span class="nv">style</span><span class="p">:</span> <span class="kt">UIBarButtonItem</span><span class="o">.</span><span class="kt">Style</span><span class="p">,</span> <span class="nv">handler</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">()</span> <span class="o">-&gt;</span> <span class="kt">Void</span><span class="p">)</span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Parameters</h4>
                        <table class="graybox">
                          <tbody>
                            <tr>
                              <td>
                                <code>
                                <em>image</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The image to use for the button</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>style</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The <code>UIBarButtonItemStyle</code> of the button</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>handler</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The closure that is called when the button is tapped</p>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:So15UIBarButtonItemC8ClosuresE5image19landscapeImagePhone5style7handlerABSo7UIImageCSg_AJSo0abC5StyleVyyctcfc"></a>
                    <a name="//apple_ref/swift/Method/init(image:landscapeImagePhone:style:handler:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:So15UIBarButtonItemC8ClosuresE5image19landscapeImagePhone5style7handlerABSo7UIImageCSg_AJSo0abC5StyleVyyctcfc">init(image:landscapeImagePhone:style:handler:)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>A convenience initializer for a UIBarButtonItem so that the tap event can
be handled with a closure. This is equivalent of using the init(image:landscapeImagePhone:style:target:action:)
initializer.</p>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">@available(iOS 5.0, *)</span>
<span class="kd">convenience</span> <span class="nf">init</span><span class="p">(</span><span class="nv">image</span><span class="p">:</span> <span class="kt">UIImage</span><span class="p">?,</span> <span class="nv">landscapeImagePhone</span><span class="p">:</span> <span class="kt">UIImage</span><span class="p">?,</span> <span class="nv">style</span><span class="p">:</span> <span class="kt">UIBarButtonItem</span><span class="o">.</span><span class="kt">Style</span><span class="p">,</span> <span class="nv">handler</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">()</span> <span class="o">-&gt;</span> <span class="kt">Void</span><span class="p">)</span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Parameters</h4>
                        <table class="graybox">
                          <tbody>
                            <tr>
                              <td>
                                <code>
                                <em>image</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The image to use for the button</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>landscapeImagePhone</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The image to use for the compressed landscape bar item</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>style</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The <code>UIBarButtonItemStyle</code> of the button</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>handler</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The closure that is called when the button is tapped</p>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:So15UIBarButtonItemC8ClosuresE5title5style7handlerABSSSg_So0abC5StyleVyyctcfc"></a>
                    <a name="//apple_ref/swift/Method/init(title:style:handler:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:So15UIBarButtonItemC8ClosuresE5title5style7handlerABSSSg_So0abC5StyleVyyctcfc">init(title:style:handler:)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>A convenience initializer for a UIBarButtonItem so that the tap event can
be handled with a closure. This is equivalent of using the init(title:style:target:action:)
initializer.</p>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">convenience</span> <span class="nf">init</span><span class="p">(</span><span class="nv">title</span><span class="p">:</span> <span class="kt">String</span><span class="p">?,</span> <span class="nv">style</span><span class="p">:</span> <span class="kt">UIBarButtonItem</span><span class="o">.</span><span class="kt">Style</span><span class="p">,</span> <span class="nv">handler</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">()</span> <span class="o">-&gt;</span> <span class="kt">Void</span><span class="p">)</span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Parameters</h4>
                        <table class="graybox">
                          <tbody>
                            <tr>
                              <td>
                                <code>
                                <em>title</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The text to use for the button</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>style</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The <code>UIBarButtonItemStyle</code> of the button</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>handler</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The closure that is called when the button is tapped</p>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:So15UIBarButtonItemC8ClosuresE03barb6SystemC07handlerABSo0abfC0V_yyctcfc"></a>
                    <a name="//apple_ref/swift/Method/init(barButtonSystemItem:handler:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:So15UIBarButtonItemC8ClosuresE03barb6SystemC07handlerABSo0abfC0V_yyctcfc">init(barButtonSystemItem:handler:)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>A convenience initializer for a UIBarButtonItem so that the tap event can
be handled with a closure. This is equivalent of using the init(barButtonSystemItem:target:action:)
initializer.</p>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">convenience</span> <span class="nf">init</span><span class="p">(</span><span class="n">barButtonSystemItem</span> <span class="nv">systemItem</span><span class="p">:</span> <span class="kt">UIBarButtonItem</span><span class="o">.</span><span class="kt">SystemItem</span><span class="p">,</span> <span class="nv">handler</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">()</span> <span class="o">-&gt;</span> <span class="kt">Void</span><span class="p">)</span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Parameters</h4>
                        <table class="graybox">
                          <tbody>
                            <tr>
                              <td>
                                <code>
                                <em>barButtonSystemItem</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The <code>UIBarButtonSystemItem</code> to be used for the button</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>handler</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The closure that is called when the button is tapped</p>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:So15UIBarButtonItemC8ClosuresE5onTap7handleryyyc_tF"></a>
                    <a name="//apple_ref/swift/Method/onTap(handler:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:So15UIBarButtonItemC8ClosuresE5onTap7handleryyyc_tF">onTap(handler:)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>This method is a convenience method to add a closure handler to a <code>UIBarButtonItem</code>.
Use this method if you are creating a <code>UIBarButtonItem</code> using an initializer
other than the convience ones provide, or if the item was created by a
Storyboard or xib.</p>

<hr>
<h4 id='an-example-that-adds-a-closure-handler-to-an-existing-code-uibarbuttonitem-code' class='heading'>An example that adds a closure handler to an existing <code>UIBarButtonItem</code>:</h4>
<pre class="highlight swift"><code><span class="n">myBarButtonItem</span><span class="o">.</span><span class="n">onTap</span> <span class="p">{</span>
    <span class="c1">// bar button tapped code</span>
<span class="p">}</span>
</code></pre>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">func</span> <span class="nf">onTap</span><span class="p">(</span><span class="nv">handler</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">()</span> <span class="o">-&gt;</span> <span class="kt">Void</span><span class="p">)</span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Parameters</h4>
                        <table class="graybox">
                          <tbody>
                            <tr>
                              <td>
                                <code>
                                <em>handler</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The closure that will be called when the tap is recognized.</p>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                    </section>
                  </div>
                </li>
              </ul>
            </div>
          </section>
        </section>
        <section id="footer">
          <p>2017 Vincent Hesener</p>
          <p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.10.0</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
        </section>
      </article>
    </div>
  </body>
</div>
</html>
